from flask import Flask,render_template,request,jsonify
import RPi.GPIO as GPIO


from components.motor import *

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.cleanup()

motor=Motor()

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def main():
    return render_template("motor.html")   # 返回网页

@app.route('/front', methods=['GET'])
def front():
    motor.action(Motor.GO)
    data = request.args.get('action')
    return jsonify({'status':data })
   
@app.route('/stop', methods=['GET'])
def stop():
    motor.action(Motor.STOP)
    data = request.args.get('action')
    return jsonify({'status':data })
 
@app.route('/turn_left', methods=['GET'])
def turn_left():
    motor.action(Motor.LEFT)
    data = request.args.get('action')
    return jsonify({'status':data })
   

@app.route('/turn_right', methods=['GET'])
def turn_right():
    motor.action(Motor.RIGHT)
    data = request.args.get('action')
    return jsonify({'status':data })
   

if __name__ == "__main__":
    app.run(port=5005)
    GPIO.cleanup()
